package org.jeecg.modules.gis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.gis.entity.TilePbf;
import org.jeecg.modules.gis.service.impl.PbfTileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/gis/pbftile")
public class PbfTileController {
    @Autowired
    private PbfTileServiceImpl pbfTileServiceImpl;

    /**
     * 根据 z, x, y 查询 PBF 二进制数据
     *
     * @param z 瓦片层级
     * @param x 瓦片 X 坐标
     * @param y 瓦片 Y 坐标
     * @return 二进制 PBF 数据
     */
    @GetMapping("/{layer}/{z}/{x}/{y}")
    public ResponseEntity<byte[]> queryByTile(@PathVariable("layer") String layer,
                                              @PathVariable("z") String z,
                                              @PathVariable("x") String x,
                                              @PathVariable("y") String y) {
        QueryWrapper<TilePbf> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("z", z).eq("x", x).eq("y", y);
        TilePbf tilePbf = pbfTileServiceImpl.getOneFromTargetTable(queryWrapper,layer);
        byte[] pbfData = tilePbf.getPbf();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("application/vnd.mapbox-vector-tile"));

        return new ResponseEntity<>(pbfData, headers, HttpStatus.OK);

    }
}
